<?php
/*******************************************\
| NUKEVIET CONTENT MANAGER SYSTEM |
| version 2.0 (c)2004-2007 Nukeviet Group |
| info@nukeviet.vn  http://nukeviet.vn - http://xunguoi.com|
|---------------------------------------------------------------------|
| Coded by LTN
| created:  2007.10.06 14:06 |
\*******************************************/

if (!defined('NV_ADMIN')) {
    die ("Access Denied");
}



$checkmodname = "Story";
$adm_access = checkmodac("".$checkmodname."");
if ($adm_access == 1) {
	
include_once("language/story_".$currentlang.".php");

/*********************************************************/
/* Users Functions                                         */
/*********************************************************/

function Story_Menu() {
	global $datafold, $adminfile;
	OpenTable();
	echo"<center>";
	echo"<b><a href=\"".$adminfile.".php?op=Story_Home\">Story Manage</a><br><br><a href=\"".$adminfile.".php?op=Story_Config\">"._STR_CONFIG."</a> | <a href=\"".$adminfile.".php?op=Story_Cat\">"._STR_CAT."</a> | <a href=\"".$adminfile.".php?op=Story_Add\">"._STR_POST."</a> | <a href=\"".$adminfile.".php?op=Story_Change_Cat\">"._STR_CHANGE."</a></b>";
	CloseTable();
	echo"</center><br>";
}	

function Story_Home() {
	global $datafold, $db, $prefix, $adminfile;
	include("../header.php");
	Story_Menu();
	$numsprd = $db->sql_fetchrow($db->sql_query("SELECT COUNT(*) FROM ".$prefix."_truyen"));
    $all_page = ( $numsprd[0] ) ? $numsprd[0] : 1;
    $per_page = 20;
    $page = ( isset($_GET['page']) ) ? intval($_GET['page']) : 0;
	 $sql3 = "SELECT id, title, alanguage FROM ".$prefix."_truyen ORDER BY id DESC LIMIT $page,$per_page";
    $result3 = $db->sql_query($sql3);
    if ($db->sql_numrows($result3) != 0) {
    OpenTable();
    echo "<center><b>"._LASTSTR."</b></center><br>";
    echo "<center><table border=\"1\" width=\"100%\" bgcolor=\"$bgcolor1\">";
    while ($row3 = $db->sql_fetchrow($result3)) {
        $id = $row3['id'];
        $title = $row3['title'];
        $alanguage = $row3['alanguage'];
        if ($alanguage == "") {
            $alanguage = ""._ALL."";
        }
        echo "<tr><td align=\"right\"><b>$id</b>"
            ."</td><td align=\"left\" width=\"100%\"><a href=\"../index.php?nv=Story&td=detail&id=$id\" target=\"_blank\">$title</a>"
            ."</td><td align=\"center\">$alanguage"
            ."</td><td align=\"right\" nowrap>(<a href=\"".$adminfile.".php?op=truyen_edit&id=$id\">"._EDIT."</a>-<a href=\"".$adminfile.".php?op=remove_truyen&id=$id\">"._DELETE."</a>)"
            ."</td></tr>";
    }
    echo "</table><br>";
    echo generate_page("".$adminfile.".php?op=Story_Home", $all_page, $per_page, $page);

        echo "<br><br><center>"
            ."<form action=\"".$adminfile.".php\" method=\"post\">"
            .""._STRID.": <input type=\"text\" NAME=\"id\" SIZE=\"10\"> "
            ."<select name=\"op\">"
            ."<option value=\"truyen_edit\" SELECTED>"._EDIT."</option>"
            ."<option value=\"remove_truyen\">"._DELETE."</option>"
            ."</select> "
            ."<input type=\"submit\" value=\""._GO."\">"
            ."</form></center>";
    CloseTable();
    }
	include("../footer.php");
}

function Story_Cat() {
	global $datafold, $db, $prefix, $adminfile;
	include("../header.php");
	Story_Menu();
	OpenTable();
	echo"<center>";
	echo "<form action=\"".$adminfile.".php\" method=\"POST\">";
	echo "<table border=\"1\" width=\"\" cellpadding=\"10\" style=\"border-collapse: collapse\">\n";
	echo "	<tr>\n";
	echo "		<td align=\"center\"><b>"._STR_ADD_CAT."</b><br><br><b>"._CAT_NAME.":</b> <input type=\"text\" size=\"30\" name=\"cat_name\" style=\"border: 1px solid #C0C0C0\">\n";
	echo"<input type=\"hidden\" name=\"op\" value=\"story_save_cat\">";
	echo "		<input type=\"submit\" size=\"30\" value=\""._ADD."\" style=\"border: 1px solid #C0C0C0\"><br></td>\n";
	echo "	</tr>\n";
	echo "</table>\n";
	echo"</form>";
	echo"</center>";
	CloseTable();
	echo"<br>";
	$res = $db->sql_query("SELECT*FROM ".$prefix."_truyen_cat ORDER BY weight ASC");
	if($db->sql_numrows($res) > 0) {
	OpenTable();
    echo "<table border=\"1\" width=\"100%\"><tr>"
    ."<td align=\"center\"><b>"._CATEGORIES."</b></td><td colspan=\"2\" align=\"center\">"
    ."<b>"._POSITION."</b></td><td align=\"center\"><b>"._FUNCTIONS."</b></td><td align=\"center\"><b>"._NUMS_STORY."</b></td></tr>";
    while ($row = $db->sql_fetchrow($res)) {
    $catid = intval($row['cid']);
    $title = $row['title'];
    $weight = $row['weight'];
    $weight1 = $weight - 1;
    $weight3 = $weight + 1;
    $sql1 = "select cid from ".$prefix."_truyen_cat where weight='$weight1'";
    $result1 = $db->sql_query($sql1);
    $row1 = $db->sql_fetchrow($result1);
    $catid1 = $row1['cid'];
    $con1 = "$catid1";
    $sql3 = "select cid from ".$prefix."_truyen_cat where weight='$weight3'";
    $result3 = $db->sql_query($sql3);
    $row3 = $db->sql_fetchrow($result3);
    $catid3 = $row3['cid'];
    $con3 = "$catid3";
    if ($con1) {
    $up = "<a href=\"".$adminfile.".php?op=OrderTruyenCategory&amp;weight=$weight&amp;catidori=$catid&amp;weightrep=$weight1&amp;catidrep=$con1\"><img src=\"../images/up.gif\" alt=\""._CATUP."\" title=\""._CATUP."\" border=\"0\" hspace=\"3\"></a>";
    }
    else {
    $up = "";
    }
    if ($con3) {
    $down = "<a href=\"".$adminfile.".php?op=OrderTruyenCategory&amp;weight=$weight&amp;catidori=$catid&amp;weightrep=$weight3&amp;catidrep=$con3\"><img src=\"../images/down.gif\" alt=\""._CATDOWN."\" title=\""._CATDOWN."\" border=\"0\" hspace=\"3\"></a>";
    }
    else {
    $down = "";
    }
    $up_down = "$up $down";
    if ((!$up) AND (!$down)) {
    $up_down = "&nbsp;";
    }
    $functions = "[ <a href=\"".$adminfile.".php?op=truyen_edit_cat&cid=$catid\">"._EDIT."</a> | <a href=\"".$adminfile.".php?op=truyen_del_cat&cid=$catid\">"._DELETE."</a> ]";
    echo "<tr><td align=\"center\"><b>$title</b></td><td align=\"center\">$weight</td><td align=\"center\">$up_down</td><td align=\"center\">$functions</td><td align=\"center\">".$row['strcount']."</td></tr>";
    }
    echo "</table>";
    CloseTable();	
	
     }
	include("../footer.php");
}	

function story_save_cat($cat_name) {
	global $datafold, $db, $prefix, $adminfile;
	$cat_name = stripslashes(FixQuotes($cat_name));
	if($cat_name =="") {
	include("../header.php");
	Story_Menu();
	echo"<center>";
	echo""._STR_NOTES1."<br>"._GOBACK."";
	echo"</center>";
	include("../footer.php");
	}else{
	list($xcid) = $db->sql_fetchrow($db->sql_query("SELECT max(cid) AS xcid FROM ".$prefix."_truyen_cat"));
	if ($xcid == "-1") { $cid = 1; } else { $cid = $xcid+1; }	
	
	list($newweight) = $db->sql_fetchrow($db->sql_query("SELECT max(weight) AS newweight FROM ".$prefix."_truyen_cat"));
    if ($newweight == "-1") { $weight = 1; } else { $weight = $newweight+1; }	
	
	$result = $db->sql_query("INSERT INTO ".$prefix."_truyen_cat VALUES('$cid', '$cat_name', '$weight', '')");
	if($result) {
    fixweightcat_story();
    fix();
    include("../header.php");
	Story_Menu();
	OpenTable();
    echo "<br><br><b><center>"._ADDED."</b></center><br><br>";
    echo "<META HTTP-EQUIV=\"refresh\" content=\"3;URL=".$adminfile.".php?op=Story_Cat\">";
    CloseTable();
	include("../footer.php");
    }else{
    include("../header.php");	
    CloseTable();
    echo "<br><br><b><center>"._STR_ERROR2."</b><br><br>"._GOBACK."</center><br><br>";
    CloseTable();
	include("../footer.php");
    	}
	}	
}

function truyen_edit_cat($cid) {
	global $datafold, $db, $prefix, $adminfile;
	$cid = intval($cid);
	if($cid =="" || $cid == 0) {
	header("Location: ".$adminfile.".php");
	exit;
	}	
	
	$res = $db->sql_query("SELECT title FROM ".$prefix."_truyen_cat WHERE cid='$cid'");
	list($title) = $db->sql_fetchrow($res);
	include("../header.php");
	Story_Menu();
	OpenTable();
	echo"<center>";
	echo "<form action=\"".$adminfile.".php\" method=\"POST\">";
	echo "<table border=\"1\" width=\"\" cellpadding=\"10\" style=\"border-collapse: collapse\">\n";
	echo "	<tr>\n";
	echo "		<td align=\"center\"><b>"._STR_EDIT_CAT."</b><br><br><b>"._CAT_NAME.":</b><input type=\"text\" size=\"30\" name=\"cat_name\" value=\"$title\" style=\"border: 1px solid #C0C0C0\">\n";
	echo"<input type=\"hidden\" name=\"cid\" value=\"$cid\">";
	echo"<input type=\"hidden\" name=\"op\" value=\"story_save_editcat\">";
	echo "		<input type=\"submit\" size=\"30\" value=\""._SAVECHANGES."\" style=\"border: 1px solid #C0C0C0\"><br></td>\n";
	echo "	</tr>\n";
	echo "</table>\n";
	echo"</form>";
	echo"</center>";
	CloseTable();
	include("../footer.php");
}		

function story_save_editcat($cid, $cat_name) {
	global $datafold, $adminfile, $prefix, $db;
	$cat_name = stripslashes(FixQuotes($cat_name));
	if($cat_name =="") {
	include("../header.php");
	Story_Menu();
	echo"<center>";
	echo""._STR_NOTES1."<br>"._GOBACK."";
	echo"</center>";
	include("../footer.php");
	}else{
	$result = $db->sql_query("UPDATE ".$prefix."_truyen_cat SET title='$cat_name' WHERE cid='$cid'");
	if($result) {
    include("../header.php");
	Story_Menu();
	OpenTable();
    echo "<br><br><b><center>"._EDITED."</b></center><br><br>";
    echo "<META HTTP-EQUIV=\"refresh\" content=\"3;URL=".$adminfile.".php?op=Story_Cat\">";
    CloseTable();
	include("../footer.php");
    }else{
    include("../header.php");	
    CloseTable();
    echo "<br><br><b><center>"._STR_ERROR2."</b><br><br>"._GOBACK."</center><br><br>";
    CloseTable();
	include("../footer.php");
    	}
	}	
}	

function truyen_del_cat($cid, $ok) {
	global $datafold, $db, $prefix, $adminfile;
	include("../".$datafold."/story_config.php");
	$cid = intval($cid);
	if($cid =="" || $cid == 0) {
	header("Location: ".$adminfile.".php");
	exit;
	}	
	
	if($ok == 1) {
	$res_img = $db->sql_query("SELECT images FROM ".$prefix."_truyen WHERE cid='$cid'");
	while(list($images) = $db->sql_fetchrow($res_img)) {
	@unlink("../$path_pic/$images");
	@unlink("../$thumb_path_pic/$images");
	}		
	$db->sql_query("DELETE FROM ".$prefix."_truyen WHERE cid='$cid'");
	$db->sql_query("DELETE FROM ".$prefix."_truyen_cat WHERE cid='$cid'");
	fixweightcat_story();
	fix();
	header("Location: ".$adminfile.".php?op=Story_Cat");
	}else{
	$res = $db->sql_query("SELECT title FROM ".$prefix."_truyen_cat WHERE cid='$cid'");
	list($title) = $db->sql_fetchrow($res);
	include("../header.php");		
	Story_Menu();	
	OpenTable();
	echo"<center>"._NOTES_DEL." <b>$title</b>?<br>"._NOTES_DEL2."<br><a href=\"".$adminfile.".php?op=Story_Change_Cat\"><b>"._CHANGE_CAT."</b></a><br><br><br><a href=\"".$adminfile.".php?op=truyen_del_cat&cid=$cid&ok=1\">"._YES."</a> | <a href=\"".$adminfile.".php?op=Story_Cat\">"._NO."</a></center>";
	CloseTable();	
	include("../footer.php");	
	}	
}

function Story_Change_Cat() {
	global $datafold, $adminfile, $prefix, $db;
	include("../header.php");		
	Story_Menu();	
	OpenTable();
	$res = $db->sql_query("SELECT cid, title FROM ".$prefix."_truyen_cat ORDER BY weight");
	while(list($cid2, $title) = $db->sql_fetchrow($res)) {
		
	$danhsach .= "<option value='$cid2'>$title</option>";
	}
	echo"<center><form method='POST' action='".$adminfile.".php' enctype='multipart/form-data'>";
    echo"<table border='1' cellpadding='3' cellspacing='3' style='border-collapse: collapse' bordercolor='#000000'>";
    echo"<tr>";
    echo"<td><b>"._FROMCAT.":</b></td>";
    echo"<td><select name='cid_from'>";
    echo"<option value='0'>"._INCAT0."</option>";
    echo"$danhsach";
    echo"</select></td>";
    echo"</tr>";
    echo"<tr>";
    echo"<td><b>"._INCAT.":</b></td>";
    echo"<td><select name='cid_in'>";
    echo"<option value='0'>"._INCAT0."</option>";
    echo"$danhsach";
    echo"</select></td>";
    echo"</tr>";
    echo"</table>";
    echo"<input type='hidden' name='op' value='strsave_chang'>";
    echo"<p align='center'><input type='submit' value='"._SAVECHANGES."'></p>";
    echo"</form></center>";	
    CloseTable();	
	include("../footer.php");	
}	

function strsave_chang() {
	global $datafold, $adminfile, $db, $prefix;
     $from_cat = intval($_POST['cid_from']);
     $in_cat = intval($_POST['cid_in']);
     if($in_cat != $from_cat) {
     $db->sql_query("UPDATE ".$prefix."_truyen SET cid = '$in_cat'  WHERE cid='$from_cat'");
     fix();
     }
     Header("Location: ".$adminfile.".php?op=Story_Cat");
     }	

function Story_Add() {
	global $datafold, $adminfile, $prefix, $db, $multilingual, $currentlang, $editor;
	include("../header.php");
	Story_Menu();
	OpenTable();
	echo "<form enctype=\"multipart/form-data\" action=\"".$adminfile.".php\" method=\"POST\">";
	echo "<center><table border=\"1\" width=\"\" cellspacing=\"0\" cellpadding=\"5\" style=\"border-collapse: collapse\">\n";
	echo "	<tr>\n";
	echo "		<td colspan=\"2\" align=\"center\"><b>"._STR_POST."</b></td>\n";
	echo "	</tr>\n";
	echo "	<tr>\n";
	echo "		<td align=\"right\"><b>"._TITLE.":</b></td>\n";
	echo "		<td>\n";
	echo "		<input type=\"text\" size=\"50\" style=\"border: 1px solid #C0C0C0\" name=\"str_title\"></td>\n";
	echo "	</tr>\n";
	echo "	<tr>\n";
	echo "		<td align=\"right\"><b>"._SOURCE.":</b></td>\n";
	echo "		<td>\n";
	echo "		<input type=\"text\" size=\"30\" style=\"border: 1px solid #C0C0C0\" name=\"str_source\"></td>\n";
	echo "	</tr>\n";
	echo "	<tr>\n";
	echo "		<td align=\"right\"><b>"._INCAT.":</b></td>\n";
	echo "		<td>";
	echo"<select name=\"cat_id\">";
	echo"<option name=\"cat_id\" value=\"0\">"._INCAT0."</option>";
	$res = $db->sql_query("SELECT cid, title FROM ".$prefix."_truyen_cat ORDER BY weight");
	while(list($cid, $title) = $db->sql_fetchrow($res)) {
	echo"<option value='$cid'>$title</option>";
	}
	echo"</select>";
	echo"</td>\n";
	echo "	</tr>\n";
	if ($multilingual == 1) {
	echo "	<tr>\n";
	echo "		<td align=\"right\"><b>"._LANGUAGE."</b></td>\n";
	echo "		<td>\n";
	
        echo"<select name=\"alanguage\"><option value=\"\">"._ALL."</option>";
        echo select_language($currentlang);
        echo "</select>";
	echo"</td>\n";
	echo "	</tr>\n";
	 } else {
        echo "<input type=\"hidden\" name=\"alanguage\" value=\"$language\">";
    }
	echo "	<tr>\n";
	echo "		<td valign=\"top\" align=\"right\"><b>"._HOMETEXT.":</b></td>\n";
	echo "		<td>\n";
	if($editor == 1) {
   		aleditor("hometext","",400,150);
    } else {
    echo "<textarea name=\"hometext\" rows=\"10\" cols=\"30\" style=\"width=500px\"></textarea>\n";
    }
	echo "			</td>\n";
	echo "	</tr>\n";
	echo "	<tr>\n";
	echo "		<td valign=\"top\" align=\"right\"><b>"._BODYTEXT.":</b></td>\n";
	echo "		<td>\n";
	if($editor == 1) {
    	aleditor("bodytext","",400,250);
    } else {
    echo "<textarea name=\"hometext\" rows=\"10\" cols=\"30\" style=\"width=500px\"></textarea>\n";
    }
	echo "			</td>\n";
	echo "	</tr>\n";
	echo "	<tr>\n";
	echo "		<td align=\"right\"><b>"._PHOTO_UPLOAD.":</b></td>\n";
	echo "		<td><input type=\"file\" name=\"userfile\" size=\"40\"></td>\n";
	echo "	</tr>\n";
	echo "	<tr>\n";
	echo "		<td colspan=\"2\" align=\"center\">";
	echo"<input type=\"hidden\" name=\"op\" value=\"truyen_post\">";
	echo "		<input type=\"submit\" size=\"30\" value=\""._ADD."\" style=\"border: 1px solid #C0C0C0\">\n";
	echo"</td>\n";
	echo "	</tr>\n";
	echo "	</table></center>\n";
	CloseTable();	
	include("../footer.php");
}	

function truyen_post($str_title, $str_source, $cat_id, $alanguage, $hometext, $bodytext) {
	global $datafold, $adminfile, $prefix, $db, $multilingual, $currentlang, $editor;
	include("../".$datafold."/story_config.php");
	$str_title = ereg_replace("\"","",$str_title);
	$str_source = ereg_replace("\"","",$str_source);
	if($editor == 1) {
	$hometext = ereg_replace("\"","",$hometext);
	$bodytext = ereg_replace("\"","",$bodytext);
	}else{
	$hometext = nl2br(ereg_replace("\"","",$hometext));
	$bodytext = nl2br(ereg_replace("\"","",$bodytext));
	}	
	$cat_id = intval($cat_id);
	if($str_title =="") {
	include("../header.php");
	Story_Menu();
	echo"<center>";
	echo""._STR_NOTES2."<br>"._GOBACK."";
	echo"</center>";
	include("../footer.php");
	exit;
	}	
	
	if($cat_id == 0) {
	include("../header.php");
	Story_Menu();
	echo"<center>";
	echo""._STR_NOTES3."<br>"._GOBACK."";
	echo"</center>";
	include("../footer.php");
	exit;
	}	
	
	if($hometext =="") {
	include("../header.php");
	Story_Menu();
	echo"<center>";
	echo""._STR_NOTES4."<br>"._GOBACK."";
	echo"</center>";
	include("../footer.php");
	exit;
	}	
	
	$ext_allow = array("jpg","jpeg","bmp","gif");
    
    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    	$images = @uploadimg("", "", 1, $sizenews, $path_pic);
    }
	
	list($xid) = $db->sql_fetchrow($db->sql_query("SELECT max(id) AS xid FROM ".$prefix."_truyen"));
    if ($xid == "-1") { $id = 1; } else { $id = $xid +1; }
	$result = $db->sql_query("INSERT INTO ".$prefix."_truyen VALUES('$id', '$cat_id', '$str_title', '$str_source', '$alanguage', '$hometext', '$bodytext', '$images', '')");
	if($result) {
	fix();	
    include("../header.php");
	Story_Menu();
	OpenTable();
    echo "<br><br><b><center>"._ADDED."</b></center><br><br>";
    echo "<META HTTP-EQUIV=\"refresh\" content=\"3;URL=".$adminfile.".php?op=Story_Add\">";
    CloseTable();
	include("../footer.php");
    }else{
    include("../header.php");	
    CloseTable();
    echo "<br><br><b><center>"._STR_ERROR2."</b><br><br>"._GOBACK."</center><br><br>";
    CloseTable();
	include("../footer.php");
    	}
}	

function truyen_edit($id) {
	global $datafold, $db, $prefix, $adminfile, $editor, $multilingual, $currentlang, $language;
	include("../".$datafold."/story_config.php");
	$id = intval($id);
	if($id =="" || $id == 0) {
	header("Location: ".$adminfile.".php");
	exit;
	}	
	
	$res_str = $db->sql_query("SELECT cid, title, alanguage, hometext, bodytext, images, nguon FROM ".$prefix."_truyen WHERE id='$id'");
	list($catid, $title, $alanguage, $hometext, $bodytext, $images, $nguon) = $db->sql_fetchrow($res_str);
	include("../header.php");
	Story_Menu();
	OpenTable();
	echo "<form enctype=\"multipart/form-data\" action=\"".$adminfile.".php\" method=\"POST\">";
	echo "<center><table border=\"1\" width=\"\" cellspacing=\"0\" cellpadding=\"5\" style=\"border-collapse: collapse\">\n";
	echo "	<tr>\n";
	echo "		<td colspan=\"2\" align=\"center\"><b>"._STR_EDIT."</b></td>\n";
	echo "	</tr>\n";
	echo "	<tr>\n";
	echo "		<td align=\"right\"><b>"._TITLE.":</b></td>\n";
	echo "		<td>\n";
	echo "		<input type=\"text\" size=\"50\" style=\"border: 1px solid #C0C0C0\" name=\"str_title\" value=\"$title\"></td>\n";
	echo "	</tr>\n";
	echo "	<tr>\n";
	echo "		<td align=\"right\"><b>"._SOURCE.":</b></td>\n";
	echo "		<td>\n";
	echo "		<input type=\"text\" size=\"30\" style=\"border: 1px solid #C0C0C0\" name=\"nguon\" value=\"$nguon\"></td>\n";
	echo "	</tr>\n";
	echo "	<tr>\n";
	echo "		<td align=\"right\"><b>"._INCAT.":</b></td>\n";
	echo "		<td>";
	echo"<select name=\"cat_id\">";
	echo"<option name=\"cat_id\" value=\"0\">"._INCAT0."</option>";
	$res = $db->sql_query("SELECT cid, title FROM ".$prefix."_truyen_cat ORDER BY weight");
	while(list($cid, $title) = $db->sql_fetchrow($res)) {
	if($catid == $cid) {
	$sel =" selected";
	}else{
	$sel ="";
	}			
	echo"<option value='$cid'$sel>$title</option>";
	}
	echo"</select>";
	echo"</td>\n";
	echo "	</tr>\n";
	if ($multilingual == 1) {
	echo "	<tr>\n";
	echo "		<td align=\"right\"><b>"._LANGUAGE."</b></td>\n";
	echo "		<td>\n";
	
        echo"<select name=\"alanguage\"><option value=\"\">"._ALL."</option>";
        echo select_language($alanguage);
        echo "</select>";
	echo"</td>\n";
	echo "	</tr>\n";
	 } else {
        echo "<input type=\"hidden\" name=\"alanguage\" value=\"$language\">";
    }
	echo "	<tr>\n";
	echo "		<td valign=\"top\" align=\"right\"><b>"._HOMETEXT.":</b></td>\n";
	echo "		<td>\n";
	if($editor == 1) {
    	aleditor("hometext",$hometext,400,150);
    } else {
    echo "<textarea name=\"hometext\" rows=\"10\" cols=\"30\" style=\"width=500px\"></textarea>\n";
    }
	echo "			</td>\n";
	echo "	</tr>\n";
	echo "	<tr>\n";
	echo "		<td valign=\"top\" align=\"right\"><b>"._BODYTEXT.":</b></td>\n";
	echo "		<td>\n";
	if($editor == 1) {
    	aleditor("bodytext",$bodytext,400,250);
    } else {
    echo "<textarea name=\"hometext\" rows=\"10\" cols=\"30\" style=\"width=500px\"></textarea>\n";
    }
	echo "			</td>\n";
	echo "	</tr>\n";
	if($images =="") {
	echo "	<tr>\n";
	echo "		<td align=\"right\"><b>"._PHOTO_UPLOAD.":</b></td>\n";
	echo "		<td><input type=\"file\" name=\"userfile\" size=\"40\"></td>\n";
	echo "	</tr>\n";
	}else{
	echo "	<tr>\n";
	echo "		<td align=\"right\"><b>"._DEL_IMG.":</b></td>\n";
	echo "		<td><input type=\"checkbox\" name=\"delpic\" value=\"1\">&nbsp;<a href=\"../$path_pic/$images\" target=\"_blank\">$images</a></td>\n";
	echo "	</tr>\n";	
	echo "	<tr>\n";
	echo "		<td><b>"._PHOTO_CHANGE."</b></td>\n";
	echo "		<td><input name=\"userfile\" type=\"file\" size=\"50\"></td>\n";
	echo "	</tr>\n";
	echo"<input type=\"hidden\" name=\"images\" value=\"$images\">";
	}	
	echo "	<tr>\n";
	echo "		<td colspan=\"2\" align=\"center\">";
	echo"<input type=\"hidden\" name=\"id\" value=\"$id\">";
	echo"<input type=\"hidden\" name=\"op\" value=\"save_truyen_edit\">";
	echo "		<input type=\"submit\" size=\"30\" value=\""._SAVECHANGES."\" style=\"border: 1px solid #C0C0C0\">\n";
	echo"</td>\n";
	echo "	</tr>\n";
	echo "	</table></center>\n";
	CloseTable();	
	include("../footer.php");
}	

function save_truyen_edit($id, $cat_id, $str_title, $nguon, $alanguage, $hometext, $bodytext, $images, $delpic) {
	global $datafold, $db, $prefix, $adminfile, $editor, $multilingual, $currentlang, $language;
	include("../".$datafold."/story_config.php");
	$id = intval($id);
	if($id =="" || $id == 0) {
	header("Location: ".$adminfile.".php");
	exit;
	}	
	
	$str_title = ereg_replace("\"","",$str_title);
	$str_source = ereg_replace("\"","",$str_source);
	$hometext = ereg_replace("\"","",$hometext);
	$bodytext = ereg_replace("\"","",$bodytext);
	$cat_id = intval($cat_id);
	if($str_title =="") {
	include("../header.php");
	Story_Menu();
	echo"<center>";
	echo""._STR_NOTES2."<br>"._GOBACK."";
	echo"</center>";
	include("../footer.php");
	exit;
	}	
	
	if($cat_id == 0) {
	include("../header.php");
	Story_Menu();
	echo"<center>";
	echo""._STR_NOTES3."<br>"._GOBACK."";
	echo"</center>";
	include("../footer.php");
	exit;
	}	
	
	if($hometext =="") {
	include("../header.php");
	Story_Menu();
	echo"<center>";
	echo""._STR_NOTES4."<br>"._GOBACK."";
	echo"</center>";
	include("../footer.php");
	exit;
	}	
	
	if($delpic ==1) {
	@unlink("../$path_pic/$images");
	@unlink("../$thumb_path_pic/$images");
	$images ="";
	}	
	
	$ext_allow = array("jpg","jpeg","bmp","gif");
    
    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    	$images_up = @uploadimg($images, $delpic, 1, $sizenews, $path_pic);	
    	if ($images_up && $delpic != 1) {
    		@unlink("../$path_pic/$images");
			@unlink("../$thumb_path_pic/$images");
    	} else {
    		$images_up = $images;	
    	}		
    } else {
    	$images_up = $images;
    }	
    
    $result = $db->sql_query("UPDATE ".$prefix."_truyen SET cid='$cat_id', title='$str_title', nguon='$nguon', alanguage='$alanguage', hometext='$hometext', bodytext='$bodytext', images='$images_up' WHERE id='$id'");
    if($result) {
    include("../header.php");
	Story_Menu();
	OpenTable();
    echo "<br><br><b><center>"._EDITED."</b></center><br><br>";
    echo "<META HTTP-EQUIV=\"refresh\" content=\"3;URL=".$adminfile.".php?op=Story_Home\">";
    CloseTable();
	include("../footer.php");
    }else{
    include("../header.php");	
    CloseTable();
    echo "<br><br><b><center>"._STR_ERROR2."</b><br><br>"._GOBACK."</center><br><br>";
    CloseTable();
	include("../footer.php");
    	}
	
}	

function OrderTruyenCategory($weightrep,$weight,$catidrep,$catidori) {
    global $datafold, $adminfile, $prefix, $db;
    $catidrep = intval($catidrep);
    $catidori = intval($catidori);
    $db->sql_query("update ".$prefix."_truyen_cat set weight='$weight' where cid='$catidrep'");
    $db->sql_query("update ".$prefix."_truyen_cat set weight='$weightrep' where cid='$catidori'");
    fixweightcat_story();
    Header("Location: ".$adminfile.".php?op=Story_Cat");
}

function fix() {
    global $datafold, $db, $prefix;
    $sql = "SELECT * FROM ".$prefix."_truyen_cat";
    $result = $db->sql_query($sql);
    while ($row = $db->sql_fetchrow($result)) {
    $num = $db->sql_fetchrow($db->sql_query("SELECT COUNT(*) FROM ".$prefix."_truyen WHERE cid = '$row[cid]'"));
    $db->sql_query("UPDATE ".$prefix."_truyen_cat SET strcount = '$num[0]' WHERE cid='$row[cid]'");
    }
}

function photo_fixcat() {
fix();
Header("Location: $_SERVER[HTTP_REFERER]");
}

function fixweightcat_story() {
    global $datafold, $prefix, $db;
    $resultweightcat_r = $db->sql_query("select cid from ".$prefix."_truyen_cat  order by weight");
    $weight = 0;
    while($row = $db->sql_fetchrow($resultweightcat_r)) {
    $cid = $row['cid'];
	$weight++;
    $cid = intval($cid);
	$db->sql_query("update ".$prefix."_truyen_cat set weight='$weight' where cid='$cid'");
    }
}

function remove_truyen($id) {
	global $db, $prefix, $adminfile, $datafold;
	include("../".$datafold."/story_config.php");
	$result = $db->sql_query("SELECT images FROM ".$prefix."_truyen WHERE id='".intval($id)."'");
	if (empty($id) || $db->sql_numrows($result) != 1) {
			Header("Location: ".$adminfile.".php?op=Story_Home"); exit;
	}
	
	list($images) = $db->sql_fetchrow($result);
	unlink("../$path_pic/$images");		
	unlink("../$path_pic/small_".$images);

	$db->sql_query("DELETE FROM ".$prefix."_truyen WHERE id='".intval($id)."'");
	fix();
	Header("Location: ".$adminfile.".php?op=Story_Home"); exit;
}	

function Story_Config() {
	global $adminfile, $datafold;
	include("../".$datafold."/story_config.php");	
	include("../header.php");
	Story_Menu();
	OpenTable();
	echo "<form enctype=\"multipart/form-data\" action=\"".$adminfile.".php\" method=\"POST\">";
	echo "<center><table border=\"1\" width=\"\" cellspacing=\"0\" cellpadding=\"5\" style=\"border-collapse: collapse\">\n";
	echo "<tr>\n";
	echo "<td colspan=\"2\" align=\"center\"><b>"._STR_CONFIG."</b></td>\n";
	echo "</tr>\n";
	echo "<tr>\n";
	echo "<td align=\"right\"><b>"._STR_CONFIG1.":</b></td><td><input type=\"text\" size=\"5\" style=\"border: 1px solid #C0C0C0\" name=\"cat_tablex\" value=\"$cat_table\"></td>\n";
	echo "</tr>\n";
	echo "<tr>\n";
	echo "<td align=\"right\"><b>"._STR_CONFIG2.":</b></td><td><input type=\"text\" size=\"5\" style=\"border: 1px solid #C0C0C0\" name=\"numstr_homex\" value=\"$numstr_home\"></td>\n";
	echo "</tr>\n";
	echo "<tr>\n";
	echo "<td align=\"right\"><b>"._STR_CONFIG3.":</b></td><td><input type=\"text\" size=\"5\" style=\"border: 1px solid #C0C0C0\" name=\"numstr_ccdx\" value=\"$numstr_ccd\"></td>\n";
	echo "</tr>\n";
	echo "<tr>\n";
	echo "<td align=\"right\"><b>"._STR_CONFIG4.":</b></td><td><input type=\"text\" size=\"5\" style=\"border: 1px solid #C0C0C0\" name=\"nums_perpagex\" value=\"$nums_perpage\"></td>\n";
	echo "</tr>\n";
	echo "<tr>\n";
	echo "<td align=\"right\"><b>"._STR_CONFIG5.":</b></td><td><input type=\"text\" size=\"5\" style=\"border: 1px solid #C0C0C0\" name=\"sizenewsx\" value=\"$sizenews\"> (pixels)</td>\n";
	echo "</tr>\n";
	echo "<tr>\n";
	echo "<td align=\"right\"><b>"._STR_CONFIG6.":</b></td><td><input type=\"text\" size=\"20\" style=\"border: 1px solid #C0C0C0\" name=\"path_picx\" value=\"$path_pic\"></td>\n";
	echo "</tr>\n";
	echo "<tr>\n";
	echo "<input type=\"hidden\" name=\"op\" value=\"save_story_config\">\n";
	echo "<td align=\"center\" colspan=\"2\"><input type=\"submit\" value=\""._SAVECHANGES."\"></td>\n";
	echo "</tr>\n";
	echo "</table></form>\n";
	CloseTable();
	include("../footer.php");
}	

function save_story_config($cat_tablex, $numstr_homex, $numstr_ccdx, $nums_perpagex, $sizenewsx, $path_picx) {
	global $adminfile, $datafold;
	@chmod("../$datafold/story_config.php", 0777);
    @$file = fopen("../$datafold/story_config.php", "w");

    $content = "<?php\n\n";
    $fctime = date("d-m-Y H:i:s",filectime ("../$datafold/story_config.php"));
	$fmtime = date("d-m-Y H:i:s");
	$content .= "// File: story_config.php.\n// Created: $fctime.\n// Modified: $fmtime.\n// Do not change anything in this file!\n\n";
    $content .= "if ((!defined('NV_SYSTEM')) AND (!defined('NV_ADMIN'))) {\n";
    $content .= "die();\n";
    $content .= "}\n";
    $content .= "\n";
    $content .= "\$cat_table = $cat_tablex;\n";
    $content .= "\$numstr_home = $numstr_homex;\n";
    $content .= "\$numstr_ccd = $numstr_ccdx;\n";
    $content .= "\$nums_perpage = $nums_perpagex;\n";
    $content .= "\$sizenews = $sizenewsx;\n";
    $content .= "\$path_pic = \"$path_picx\";\n\n";
    $content .= "?>";

    @fwrite($file, $content);
    @fclose($file);
    @chmod("../$datafold/story_config.php", 0604);
     Header("Location: ".$adminfile.".php?op=Story_Config"); exit;
}	

switch($op) {
	
	case "save_story_config":
	save_story_config($cat_tablex, $numstr_homex, $numstr_ccdx, $nums_perpagex, $sizenewsx, $path_picx);
	break;
	
	case "Story_Config":
	Story_Config();
	break;
	
	case "remove_truyen":
	remove_truyen($id);
	break;

    case "Story_Home":
    Story_Home();
    break;
    
    case "Story_Cat":
    Story_Cat();
    break;
    
    case "story_save_cat":
    story_save_cat($cat_name);
    break;
    
    case "OrderTruyenCategory":
    OrderTruyenCategory($weightrep,$weight,$catidrep,$catidori);
    break;
    
    case "truyen_edit_cat":
    truyen_edit_cat($cid);
    break;
    
    case "story_save_editcat":
    story_save_editcat($cid, $cat_name);
    break;
    
    case "truyen_del_cat":
    truyen_del_cat($cid, $ok);
    break;
    
    case "Story_Change_Cat":
    Story_Change_Cat();
    break;
    
    case "strsave_chang":
    strsave_chang();
    break;
    
    case "Story_Add":
    Story_Add();
    break;
    
    case "truyen_post":
    truyen_post($str_title, $str_source, $cat_id, $alanguage, $hometext, $bodytext);
    break;
    
    case "truyen_edit":
    truyen_edit($id);
    break;
    
    case "save_truyen_edit":
    save_truyen_edit($id, $cat_id, $str_title, $nguon, $alanguage, $hometext, $bodytext, $images, $delpic);
    break;
}

} else {
    echo "Access Denied";
}

?>